import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


public class CreateIndex {

	datatype[] datatypes = new datatype[7];

	String temp[];
	public void createIndex(String path, ArrayList<Integer> index) throws Exception
	{
		//System.out.println("inside create");
		datatypes[0] = new i1();
		datatypes[1] = new i2();
		datatypes[2] = new i4();
		datatypes[3] = new i8();
		datatypes[4] = new r4();
		datatypes[5] = new r8();
		datatypes[6] = new cx();
		
		heap.f = new File(path);
		int j = path.indexOf(".");
		String x = path.substring(0,j+1);
		index utility = new index();
		for(int i=0;i<index.size();i++)
		{
		File fcreate1 = new File(x+index.get(i)+".lht");
		Bucket b = new Bucket();
		b.data[0] = '`';
		File fcreate2 = new File(x+index.get(i)+".lho");
		utility.writeBucket(b, fcreate1, 0);
		}

		if(heap.f.exists())
		{
			ReadHeader reader = new ReadHeader();
			
			try {
				temp = reader.read(path);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String temp2[] = new String[temp.length-1];
			for(int q=0;q<temp2.length;q++)
			{
				temp2[q] = temp[q];
			}
			ArrayList<datatype> header = convertHeader.convertHeader(temp2);
			index [] builtindex = new index[header.size()];
			long[] details = new long [6];
			details[0]=1;
			details[1]=1;
			details[2]=0;
			details[3]=0;
			details[4]=Long.MAX_VALUE;
			details[5]=0;
			//System.out.println("Create");
			IndexHeader iH = new IndexHeader();
			for(int i=0;i<header.size();i++)
			{
				if(index.contains(i+1))
				{
					File f = new File(x+(i+1)+".lho");
					File f1 = new File(x+(i+1)+".lht");
					details[3]=8+header.get(i).length;
					builtindex[i] = new index(details);
					builtindex[i].fo =f;
					builtindex[i].ft =f1;
					iH.writeHeader(builtindex[i],details);
				}
			}
	
			int count =0;
			while(heap.readpointer<heap.f.length())
			{
				ReadNext rn = new ReadNext();
	
				String record=rn.readNext(heap.readpointer, header);
				String [] d = record.split(",");
				for(int i=0;i<d.length;i++)
				{
					if(index.contains(i+1))
					{
						try {
							builtindex[i].updateIndex(d[i],heap.readpointer-Long.parseLong(temp[temp.length-1]),header,i);
							//System.out.println("Pass"+ "\t" + d[i] + "\t" + count++);
						/*	for(int w =0;w<builtindex[i].actualSize;w++)
							{
								
								System.out.println("Bucket"+w);
								builtindex[i].Showbuckets(w, header, i);
							}
							*/
							
							
							
							//System.out.println("Inserted" + i);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			
			
			//long temp3[];
			for(int i=0;i<header.size();i++)
			{
				if(index.contains(i+1))
				{
					iH.writeHeader(builtindex[i]);
					//System.out.print(false);
				}
			}
		/*	for(int i=0;i<header.size();i++)
			{
				if(index.contains(i+1))
				{
					long temp5[] = new long [6];
					temp5 = iH.readHeader((builtindex[i].fo));
					for(long t : temp5)
					System.out.print(t+ "\t");
					System.out.println();
				}
			}
			*/
			
			
			
		}
	
		/*else
		{
			FileAccess access = new FileAccess();
			access.readFile(path);
		}
		*/
	}
}
